import Cookies from 'js-cookie'
import config from '@/config'

export default {
  // 令牌操作
  setToken: function (token) {
    Cookies.set(config.tokenCookieKey, token, { expires: config.cookieExpires || 1 })
  },
  getToken: function () {
    const token = Cookies.get(config.tokenCookieKey)
    if (token) return token
    else return false
  },
  // redirectUriCookieKey
  getRedirectUri: function () {
    const val = Cookies.get(config.redirectUriCookieKey)
    if (val) return val
    else return false
  },
  setRedirectUri: function (val) {
    Cookies.set(config.redirectUriCookieKey, val, { expires: 300 })
  },
  // 取地址参数
  getParameter: function (name) {
    const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
    let match = window.location.search.substr(1).match(reg)
    if (match != null) {
      return unescape(match[2])
    }
    return null
  },
  // 将对象中的数字属性转换为字符串
  convertObjectNumberPropertieToString: function (oldObject) {
    let newObject = {}
    for (let key in oldObject) {
      if (typeof oldObject[key] === 'number') {
        newObject[key] = oldObject[key] + ''
      } else {
        newObject[key] = oldObject[key]
      }
    }
    return newObject
  }
}
